#include <iostream>
#include <fstream>
#include <math.h>
#include <algorithm>
#include <string.h>

using namespace std;

ifstream f("examene.txt");
ofstream g("examene_out.txt");

int luni[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

class examen{
public:
	int zz, ll, aa;
	char nume[100];

	bool operator < (const examen &other) const {
		return aa < other.aa || 
		aa == other.aa && ll < other.ll ||
		aa == other.aa && ll == other.ll && zz < other.zz;
	}

};

class orar{
	examen ex[100];
	int nrEx;

public:
	void citire(){
		f >> nrEx;
		for(int i = 1; i <= nrEx; i ++)
			f >> ex[i].nume >> ex[i].zz >> ex[i].ll >> ex[i].aa;
	}

	int diferenta(examen a, examen b){
		if(b < a)
			swap(a, b);
		if(a.aa == b.aa){
			if(a.ll == b.ll)
				return b.zz - a.zz;
			int dif = 0;
			dif = luni[a.ll] - a.zz;
			dif += b.zz;
			for(int i = a.ll + 1; i < b.ll; i ++)
				dif += luni[i];

			return dif;
		}
		int sum = 0;
		for(int i = 1; i <= 12; i ++)
			sum += luni[i];

		int dif = luni[a.ll] - a.zz;
		for(int i = a.ll + 1; i <= 12; i ++)
			dif += luni[i];
		dif += b.zz;
		for(int i = 1; i < b.ll; i ++)
			dif += luni[i];

		dif += max(b.aa - a.aa - 1, 0) * sum;

		return dif;
	}

	void sorteaza(){
		sort(ex + 1, ex + nrEx + 1);
	}

	void afiseaza(){
		ex[0].aa = ex[0].ll = ex[0].zz = 0;
		strcpy(ex[0].nume, "Inceput");
		int ind, minim = (1 << 30);

		for(int i = 1; i <= nrEx; i ++){
			g << ex[i].nume << " " << ex[i].zz << " " << ex[i].ll << " " << ex[i].aa << " ->" << diferenta(ex[i - 1], ex[i]) << "\n";
			if(diferenta(ex[i - 1], ex[i]) < minim)
				minim = diferenta(ex[i - 1], ex[i]), ind = i;
		}

		g << "Timpul minim de pregatire :\n";
		g << ex[ind].nume << " " << ex[ind].zz << " " << ex[ind].ll << " " << ex[ind].aa;
	}
};


int main(){

	orar o;
	o.citire();
	o.sorteaza();
	o.afiseaza();

	return 0;
}